//
//  MKWelcomeViewController.m
//  Weibo_OC
//
//  Created by mac on 16/1/16.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "MKWelcomeViewController.h"

#import "MKUserModelView.h"
#import "UILabel+MKLabel.h"

#import <UIImageView+WebCache.h>
#import <Masonry.h>

@interface MKWelcomeViewController ()

@property (weak, nonatomic) UIImageView *headerImageView;
@property (weak, nonatomic) UIImageView *bacImageView;
@property (weak, nonatomic) UILabel *welTipLabel;

@property (strong, nonatomic) MKUserModelView *viewModel;

@end

@implementation MKWelcomeViewController

- (MKUserModelView *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[MKUserModelView alloc] init];
    }
    
    return _viewModel;
}

- (void)loadView {
    UIImageView *bacImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ad_background"]];
    self.bacImageView = bacImage;
    self.view = self.bacImageView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [self showAnimation];
}

- (void)setupUI {
    // 创建头像
    UIImageView *headerImageView = [[UIImageView alloc] init];
    self.headerImageView = headerImageView;
    [self.headerImageView sd_setImageWithURL:self.viewModel.headerImageURL placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    
    self.headerImageView.layer.cornerRadius = 45;
    self.headerImageView.layer.masksToBounds = YES;
    [self.view addSubview:self.headerImageView];
    
    //创建label
    NSString *tipText = [NSString stringWithFormat:@"%@ 欢迎归来",self.viewModel.name];
    UILabel *lbl = [[UILabel alloc] initWithTitle:tipText andWithTitleColor:[UIColor darkGrayColor] andWithFontSize:16] ;
    self.welTipLabel = lbl;
    self.welTipLabel.alpha = 0;
    [self.view addSubview:self.welTipLabel];
    
    //自动布局
    
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-160);
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(90);
        make.width.mas_equalTo(90);
    }];
    
    [self.welTipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headerImageView.mas_bottom).offset(20);
        make.centerX.equalTo(self.headerImageView);
    }];
    
    
    
}

- (void)showAnimation {

    CGFloat finalOffset = - [UIScreen mainScreen].bounds.size.height + 160;
    
    
    [UIView animateWithDuration:1.5 delay:0 usingSpringWithDamping:0.98 initialSpringVelocity:9.8 options:0 animations:^{
        
        [self.headerImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).offset(finalOffset);
        }];
        [self.view layoutIfNeeded];
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:1 animations:^{
            self.welTipLabel.alpha = 1;
        } completion:^(BOOL finished) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"setupRootViewController" object:nil];
        }];
    }];
}





@end

























